package com.web.music.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

import com.web.music.model.*;

/**
 * Loads XML files from local file system, extracts information from XML and saves it into database.
 */
public class LocalMusicProcessor extends BaseMusicProcessor {
	static Logger logger = Logger.getLogger(DomXmlParser.class.getName());
	
	String peopleFilePath;	// path to people.xml file
	String catalogFilePath;	// path to catalog.xml file

	public void execute(){
		super.execute();
		logger.info("[LocalMusicProcessor] Starting to extract people info from file: " + peopleFilePath);
		Map<String,Person> people = getXmlParser().extractPeopleInfo(new File(peopleFilePath));
		logger.info("[LocalMusicProcessor] People info extracted. In total [" + people.size() + "] items processed.");
		logger.info("[LocalMusicProcessor] Saving people to database");
		getPersistence().saveAll(new ArrayList<Person>(people.values()));
		logger.info("[LocalMusicProcessor] People info successfully saved into database");
		
		// TODO [done] 
		// Your task is to continue with catalog.xml file parsing.
		// All data should appear in database after application execution finishes.
		logger.info("[LocalMusicProcessor] Starting to extract genre info from file: " + catalogFilePath);
		Map<String, Genre> genres = getXmlParser().extractGenreInfo(new File(catalogFilePath));

		logger.info("[LocalMusicProcessor] Genre info extracted. In total [" + genres.size() + "] items processed.");
		logger.info("[LocalMusicProcessor] Saving genres to database");
		getPersistence().saveAll(new ArrayList<Genre>(genres.values()));
		logger.info("[LocalMusicProcessor] Genre info successfully saved into database");

		logger.info("[LocalMusicProcessor] Starting to extract Artist info from file: " + catalogFilePath);
		Map<String, Artist> artists = getXmlParser().extractArtistInfo(new File(catalogFilePath), genres, people);
		logger.info("[LocalMusicProcessor] Saving artists to database");
		getPersistence().saveAll(new ArrayList<Artist>(artists.values()));
		logger.info("[LocalMusicProcessor] Artist info successfully saved into database");

		logger.info("[LocalMusicProcessor] Starting to extract Album info from file: " + catalogFilePath);
		Map<String, Album> albums = getXmlParser().extractAlbumInfo(new File(catalogFilePath), artists, genres);
		logger.info("[LocalMusicProcessor] Album info extracted. In total [" + albums.size()+"] items processed");
		logger.info("[LocalMusicProcessor] Saving albums to database");
		getPersistence().saveAll(new ArrayList<Album>(albums.values()));
		logger.info("[LocalMusicProcessor] Album info successfully saved into database");
	}	
	
	public String getPeopleFilePath() {
		return peopleFilePath;
	}

	public void setPeopleFilePath(String peopleFilePath) {
		this.peopleFilePath = peopleFilePath;
	}

	public String getCatalogFilePath() {
		return catalogFilePath;
	}

	public void setCatalogFilePath(String catalogFilePath) {
		this.catalogFilePath = catalogFilePath;
	}
	
}
